package com.example.demo.test;

import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

/*
    https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/example/ChatServer.java

     原文链接：https://blog.csdn.net/huangyuan_xuan/article/details/109325021
 */
public class ChatServer extends WebSocketServer {
    public ChatServer(int port) throws UnknownHostException {
        super(new InetSocketAddress(port));
    }

    public ChatServer(InetSocketAddress address) {
        super(address);
    }


    @Override
    public void onOpen(WebSocket conn, ClientHandshake clientHandshake) {
        conn.send("== Welcome to the server!"); //This method sends a message to the new client
        broadcast("new connection: " + clientHandshake.getResourceDescriptor()); //This method sends a message to all clients connected
        System.out.println("== " + conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room!");

    }

    @Override
    public void onClose(WebSocket conn, int i, String s, boolean b) {
        broadcast("== " + conn + " has left the room!");
        System.out.println("== " + conn + " has left the room!");
    }

    @Override
    public void onMessage(WebSocket conn, String message) {
        broadcast(message);
        System.out.println("== " + conn + ": " + message);
    }

    @Override
    public void onError(WebSocket conn, Exception e) {
        e.printStackTrace();
        System.out.println("== onError " + e.toString());
        if (conn != null) {
            // some errors like port binding failed may not be assignable to a specific websocket
        }
    }

    @Override
    public void onStart() {
        System.out.println("== " + "Server started!");
        setConnectionLostTimeout(0);
        setConnectionLostTimeout(100);
    }


    public static void main(String[] args) {
//        try {
//            port = Integer.parseInt(args[0]);
//        } catch (Exception ex) {
//        }

        try {

            int port = 8887; // 843 flash policy port
            ChatServer s = new ChatServer(port);
            s.start();
            System.out.println("== ChatServer started on port: " + s.getPort());

            BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
                String in = sysin.readLine();
                s.broadcast(in);
                if (in.equals("exit")) {
                    s.stop(1000);
                    break;
                }
            }


        } catch (Exception e) {
            System.out.println("== ChatServer 异常信息：" + e.toString());
        }


    }

    //== ChatServer started on port: 8887
    //== Server started!
    //== 10.200.88.181 entered the room!
    //== org.java_websocket.WebSocketImpl@50aaa434 has left the room!
    //== 10.200.88.181 entered the room!
    //== org.java_websocket.WebSocketImpl@1e18ee79 has left the room!
    //== 10.200.88.181 entered the room!
    //== org.java_websocket.WebSocketImpl@349ae669 has left the room!
    //== 10.200.88.181 entered the room!
    //== org.java_websocket.WebSocketImpl@3fd712a: 你好
    //服务器正常
    //恭喜你获得1000大奖
    //== org.java_websocket.WebSocketImpl@3fd712a: 哈哈
}
